﻿using System;
using System.Data;
using System.Collections.Generic;
using MxWeiXinPF.Common;
using MxWeiXinPF.Model;
namespace MxWeiXinPF.BLL
{
	/// <summary>
	/// 购物车
	/// </summary>
    public partial class wx_shop_cart
    {
        private readonly MxWeiXinPF.DAL.wx_shop_cart dal = new MxWeiXinPF.DAL.wx_shop_cart();

        public int Add(MxWeiXinPF.Model.wx_shop_cart model)
        {
            return this.dal.Add(model);
        }

        public List<MxWeiXinPF.Model.wx_shop_cart> DataTableToList(DataTable dt)
        {
            List<MxWeiXinPF.Model.wx_shop_cart> list = new List<MxWeiXinPF.Model.wx_shop_cart>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    MxWeiXinPF.Model.wx_shop_cart item = this.dal.DataRowToModel(dt.Rows[i]);
                    if (item != null)
                    {
                        list.Add(item);
                    }
                }
            }
            return list;
        }

        public bool Delete(int id)
        {
            return this.dal.Delete(id);
        }

        public bool DeleteList(string idlist)
        {
            return this.dal.DeleteList(idlist);
        }

        public bool Exists(int id)
        {
            return this.dal.Exists(id);
        }

        public DataSet GetAllList()
        {
            return this.GetList("");
        }

        public List<cartProduct> GetCartList(string openid, int wid)
        {
            DataSet cartList = this.dal.GetCartList(openid, wid);
            List<cartProduct> list = new List<cartProduct>();
            if (((cartList != null) && (cartList.Tables.Count > 0)) && (cartList.Tables[0].Rows.Count > 0))
            {
                int count = cartList.Tables[0].Rows.Count;
                cartProduct item = new cartProduct();
                for (int i = 0; i < count; i++)
                {
                    DataRow row = cartList.Tables[0].Rows[i];
                    string theopenid = MyCommFun.ObjToStr(row["openid"]);
                    int thepid = MyCommFun.Obj2Int(row["productId"]);
                    int thewid = MyCommFun.Obj2Int(row["wid"]);
                    item = new cartProduct
                    {
                        id = MyCommFun.Obj2Int(row["id"]),
                        openid = MyCommFun.ObjToStr(row["openid"]),
                        productId = MyCommFun.Obj2Int(row["productId"]),
                        productName = MyCommFun.ObjToStr(row["productName"]),
                        productNum = MyCommFun.Obj2Int(row["productNum"]),
                        productPic = MyCommFun.ObjToStr(row["pic"]),
                        skuId = MyCommFun.Obj2Int(row["skuId"]),
                        skuInfo = MyCommFun.ObjToStr(row["skuInfo"]),
                        totPrice = MyCommFun.Str2Decimal(row["totPrice"].ToString()),
                        totPrice100 = item.totPrice * 100M,
                        saleprice = MyCommFun.Str2Decimal(row["salePrice"].ToString()),
                        markertprice = MyCommFun.Str2Decimal(row["marketPrice"].ToString()),
                        costprice = MyCommFun.Str2Decimal(row["costPrice"].ToString()),
                        shopid = MyCommFun.Obj2Int(row["shopid"]),
                        createDate = MyCommFun.Obj2DateTime(row["createDate"]),
                        wid = MyCommFun.Obj2Int(row["wid"]),
                        stock = MyCommFun.Obj2Int(row["stock"]),
                        productUrl = string.Concat(new object[] { "/shop/detail.aspx?wid=", thewid, "&pid=", thepid, "&openid=", theopenid }),
                        RatePrice = MyCommFun.Str2Decimal(row["RatePrice"].ToString()),
                        seq = i
                    };
                    item.avgPrice = MyCommFun.decimalF2(item.totPrice / ((item.productNum == 0) ? 1 : item.productNum));
                    item.avgPrice100 = item.avgPrice * 100m;
                    list.Add(item);
                }
            }
            return list;
        }

        public List<cartProduct> GetCartListByUserID(int userid, int wid)
        {
            DataSet cartListByUserID = this.dal.GetCartListByUserID(userid, wid);
            List<cartProduct> list = new List<cartProduct>();
            if (((cartListByUserID != null) && (cartListByUserID.Tables.Count > 0)) && (cartListByUserID.Tables[0].Rows.Count > 0))
            {
                int count = cartListByUserID.Tables[0].Rows.Count;
                cartProduct item = new cartProduct();
                for (int i = 0; i < count; i++)
                {
                    DataRow row = cartListByUserID.Tables[0].Rows[i];
                    item = new cartProduct
                    {
                        id = MyCommFun.Obj2Int(row["id"]),
                        openid = MyCommFun.ObjToStr(row["openid"]),
                        productId = MyCommFun.Obj2Int(row["productId"]),
                        productName = MyCommFun.ObjToStr(row["productName"]),
                        productNum = MyCommFun.Obj2Int(row["productNum"]),
                        productPic = MyCommFun.ObjToStr(row["pic"]),
                        skuId = MyCommFun.Obj2Int(row["skuId"]),
                        skuInfo = MyCommFun.ObjToStr(row["skuInfo"]),
                        totPrice = MyCommFun.Str2Decimal(row["totPrice"].ToString()),
                        totPrice100 = item.totPrice * 100M,
                        avgPrice = MyCommFun.decimalF2(item.totPrice / ((item.productNum == 0) ? 1 : item.productNum)),
                        avgPrice100 = item.avgPrice * 100M,
                        createDate = MyCommFun.Obj2DateTime(row["createDate"]),
                        wid = MyCommFun.Obj2Int(row["wid"]),
                        stock = MyCommFun.Obj2Int(row["stock"]),
                        productUrl = string.Concat(new object[] { "/shop/detail.aspx?wid=", item.wid, "&pid=", item.productId, "&openid=", item.openid }),
                        RatePrice = MyCommFun.Str2Decimal(row["RatePrice"].ToString()),
                        seq = i,
                        shopid = MyCommFun.Obj2Int(row["shopid"])
                    };
                    list.Add(item);
                }
            }
            return list;
        }

        public DataSet GetList(string strWhere)
        {
            return this.dal.GetList(strWhere);
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            return this.dal.GetList(Top, strWhere, filedOrder);
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
        }

        public int GetMaxId()
        {
            return this.dal.GetMaxId();
        }

        public MxWeiXinPF.Model.wx_shop_cart GetModel(int id)
        {
            return this.dal.GetModel(id);
        }

        public List<MxWeiXinPF.Model.wx_shop_cart> GetModelList(string strWhere)
        {
            DataSet list = this.dal.GetList(strWhere);
            return this.DataTableToList(list.Tables[0]);
        }

        public List<cartProduct> GetProductList_SaleOrder(string openid, int wid, string productlistid)
        {
            DataSet set = this.dal.GetProductList_SaleOrder(openid, wid, productlistid);
            List<cartProduct> list = new List<cartProduct>();
            if (((set != null) && (set.Tables.Count > 0)) && (set.Tables[0].Rows.Count > 0))
            {
                int count = set.Tables[0].Rows.Count;
                cartProduct item = new cartProduct();
                for (int i = 0; i < count; i++)
                {
                    DataRow row = set.Tables[0].Rows[i];
                    item = new cartProduct
                    {
                        openid = openid,
                        productId = MyCommFun.Obj2Int(row["productId"]),
                        productName = MyCommFun.ObjToStr(row["productName"]),
                        productNum = MyCommFun.Obj2Int(row["productNum"]),
                        totPrice = MyCommFun.Str2Decimal(row["totPrice"].ToString()),
                        markertprice = MyCommFun.Str2Decimal(row["totPrice"].ToString()),
                        saleprice = MyCommFun.Str2Decimal(row["salePrice"].ToString()),
                        totPrice100 = item.totPrice * 100M,
                        avgPrice = MyCommFun.decimalF2(item.totPrice / ((item.productNum == 0) ? 1 : item.productNum)),
                        avgPrice100 = item.avgPrice * 100M,
                        createDate = MyCommFun.Obj2DateTime(row["createDate"]),
                        wid = MyCommFun.Obj2Int(row["wid"]),
                        stock = MyCommFun.Obj2Int(row["stock"]),
                        productUrl = string.Concat(new object[] { "/shop/detail.aspx?wid=", item.wid, "&pid=", item.productId, "&openid=", item.openid }),
                        RatePrice = MyCommFun.Str2Decimal(row["RatePrice"].ToString()),
                        seq = i
                    };
                    list.Add(item);
                }
            }
            return list;
        }

        public int GetRecordCount(string strWhere)
        {
            return this.dal.GetRecordCount(strWhere);
        }

        public bool RemoveCartInfo(int wid, string openid)
        {
            return this.dal.RemoveCartInfo(wid, openid);
        }

        public bool RemoveCartInfoByUserID(int wid, int userid)
        {
            return this.dal.RemoveCartInfoByUserID(wid, userid);
        }

        public bool Update(MxWeiXinPF.Model.wx_shop_cart model)
        {
            return this.dal.Update(model);
        }

        public bool UpdateNum(int cartid, int newNum)
        {
            return this.dal.UpdateNum(cartid, newNum);
        }
    }
}

